﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using XPressify.Data.Helpers;

namespace XPressify.Data.CompliedQueries
{
    public class PostCompiledQueries
    {
        

        public static Func<Repository, Post, IEnumerable<Post>> GetAllCommentsGlobal
        {
            get
            {
                return CompiledQuery.Compile<Repository, Post, IEnumerable<Post>>(
                    (context, post) => context.BaseMessages.OfType<Post>().Where(
                    com =>
                    com.BaseObject.Id == post.BaseObject.Id && com.PostType == PostType.Comment &&
                    post.CommentType != CommentType.PostWithoutComments).OrderBy(com => com.Created)
                    );
            }
        }

        public static Func<Repository, Post, IEnumerable<Post>> GetAllCommentsLocal
        {
            get
            {
                return CompiledQuery.Compile<Repository, Post, IEnumerable<Post>>(
                    (context, post) => context.BaseMessages.OfType<Post>().Where(
                    com =>
                    com.Parent.Id == post.Id).OrderBy(com => com.Created)
                    );
            }
        }

        public static Func<Repository, Guid, Post> GetPostDummyAlbum
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Post>(
                    (context, albumId) => context.BaseMessages.OfType<Post>().SingleOrDefault(
                        pst => pst.BaseObject.Id == albumId && pst.PostType == PostType.PostDummy)
                    );
            }
        }

        public static Func<Repository, Guid, Post> GetPostDummyMedia
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Post>(
                    (context, mediaId) => context.BaseMessages.OfType<Post>().SingleOrDefault(
                    pst => pst.PostType == PostType.PostDummy && pst.BaseObject.Id == mediaId)
                    );
            }
        }

        public static Func<Repository, Guid, Post> GetPost
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, Post>(
                    (context, postId) => context.BaseMessages.OfType<Post>().SingleOrDefault(
                        pst => pst.Id == postId && pst.PostType == PostType.Post)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Post>> GetAllPostsByPostSubjectIdOfDesc
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Post>>(
                    (context, subjectId) => context.BaseMessages.OfType<Post>().Where(
                    pst => pst.BaseSubject.Id == subjectId && pst.PostType == PostType.Post).OrderByDescending(st => st.Created)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Post>> GetAllPostsByPostIdDesc
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Post>>(
                    (context, postId) => context.BaseMessages.OfType<Post>().Where(
                        pst => pst.Id == postId && pst.PostType == PostType.Post).OrderByDescending(st => st.Created)
                    );
            }
        }

        public static Func<Repository, Guid, IEnumerable<Post>> GetAllPostsGeneral
        {
            get
            {
                return CompiledQuery.Compile<Repository, Guid, IEnumerable<Post>>(
                    (context, subjectId) => context.BaseMessages.OfType<Post>().Where(ps => ps.BaseSubject.Id == subjectId)
                    );
            }
        }


    }
}